<template>
  <div>
    <el-container class="header">
      <!-- 头 -->
      <el-header class="father">
        <el-row :gutter="20">
          <el-col :span="21">
            <el-form
              :inline="true"
              :model="formInline"
              class="demo-form-inline"
            >
              <el-form-item label="关键字">
                <el-input
                  v-model="formInline.subjectName"
                  placeholder="根据编号搜索"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="3">
            <el-button class="confirm">清空</el-button>
            <el-button type="primary" class="cancel" @click="onSubmit"
              >搜索</el-button
            >
          </el-col>
        </el-row>
      </el-header>

      <!-- 身体 -->
      <el-main>
        <!-- 数据条数 -->
        <div class="recording">
          <span
            ><i class="el-icon-info"> </i>数据一共
            {{ tableData.counts }} 条</span
          >
        </div>
        <!-- table表格数据区域 -->
        <el-table :data="list" style="width: 100%" stripe v-loading="loading">
          <el-table-column prop="id" label="编号" align="center" width="200">
          </el-table-column>
          <el-table-column label="题型" align="center">
            <template v-slot="{ row }">
              <span>
                {{
                  row.questionType === "1"
                    ? "单选"
                    : row.questionType === "2"
                    ? "多选"
                    : row.questionType === "3"
                    ? "简答"
                    : "无"
                }}
              </span>
            </template>
          </el-table-column>
          <el-table-column width="200" label="题目编号" align="center">
            <template v-slot="{ row }">
              <el-link type="primary" @click="preview(row.id)">
                <div v-for="(item, index) in row.questionIDs" :key="index">
                  {{ item.number }}
                </div>
              </el-link>
            </template>
          </el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(s)"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="accuracyRate" label="正确率(%)" align="center">
          </el-table-column>
          <el-table-column prop="userName" label="录入人" align="center">
          </el-table-column>
          <el-table-column label="操作" align="center">
            <!-- 表格子项的作用域插槽 -->
            <template #default="{ row }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                plain
                circle
                @click="del(row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 预览 -->
        <el-dialog title="题目预览" :visible.sync="previewArticle" width="50%">
          <el-row :gutter="20" style="padding: 20px 0px">
            <el-col :span="6"><div>【题型】：</div></el-col>
            <el-col :span="6"><div>【编号】：</div></el-col>
            <el-col :span="6"><div>【难度】：</div></el-col>
            <el-col :span="6"><div>【标签】：</div></el-col>
          </el-row>
          <el-row
            :gutter="20"
            style="padding: 20px 0px; border-bottom: 1px solid #9e9e9e"
          >
            <el-col :span="6"><div>【学科】：</div></el-col>
            <el-col :span="6"><div>【目录】：</div></el-col>
            <el-col :span="6"><div>【方向】：</div></el-col>
          </el-row>
          <div style="padding: 20px 0px; border-bottom: 1px solid #9e9e9e">
            【题干】：
            <div>选项：（以下选中的选项为正确答案）</div>
          </div>
          <div
            v-if="show"
            style="padding: 20px 0px; border-bottom: 1px solid #9e9e9e"
          >
            【参考答案】：<el-button type="danger" @click="show = false"
              >视频答案预览</el-button
            >
          </div>
          <div
            v-else
            style="padding: 20px 0px; border-bottom: 1px solid #9e9e9e"
          >
            【参考答案】：<el-button type="primary" @click="show = true"
              >关闭</el-button
            >
            <div><video src="" autoplay muted controls loop></video></div>
          </div>
          <div style="padding: 20px 0px; border-bottom: 1px solid #9e9e9e">
            【答案解析】：
            <div></div>
          </div>
          <div style="padding: 20px 0px">
            【题目备注】：
            <div></div>
          </div>
        </el-dialog>

        <!-- 分页组件区域 -->
        <el-row type="flex" style="height: 60px" align="middle" justify="end">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryObj.page"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="queryObj.pagesize"
            layout="prev, pager, next, sizes, jumper"
            :total="tableData.counts"
          >
          </el-pagination>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'

export default {
  data () {
    return {
      loading: false,
      previewArticle: false,
      show: true,
      formInline: {
        subjectName: null
      },
      queryObj: {
        page: 1, // 控制当前页
        pagesize: 10, // 控制每页有几条数据
        subjectName: null
      },
      // 验证规则
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },
      tableData: {},
      list: []
    }
  },
  created () {
    this.initTable()
  },
  methods: {
    // 获取table数据
    async initTable () {
      this.loading = true
      const { data } = await randoms(this.queryObj)
      this.tableData = data
      this.list = data.items
      this.loading = false
    },

    // 删除功能
    del (id) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            await removeRandoms({ id })
            this.$message.success('删除成功')
            this.initTable()
          } catch (error) {
            console.log(error)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 查询
    onSubmit () {
      this.queryObj.subjectName = this.formInline.subjectName
      this.initTable()
      this.formInline.subjectName = null
    },
    // 分页功能
    // pagesize改变时会触发
    handleSizeChange (val) {
      this.queryObj.pagesize = val
      this.queryObj.page = 1
      this.initTable()
    },
    // page当前页改变时会触发
    async handleCurrentChange (val) {
      // 改变参数对象中的当前页
      this.queryObj.page = val
      this.initTable()
    },
    // 预览
    async preview (id) {
      this.previewArticle = true
    }
  }
}
</script>

<style scoped lang="less">
.header {
  background-color: #fff;
  margin: 10px 20px;
  border-radius: 5px;
  box-shadow: 0px 0px 20px #ccc;
  .father {
    padding: 20px 20px;
    height: 100px;
    .added {
      height: 36px;
      width: 96px;
      font-size: 12px;
      padding-left: 11px;
      margin-left: 140px;
    }
  }
}
.recording {
  height: 35px;
  width: 100%;
  padding: 8px 16px;
  background-color: #f4f4f5;
  border-radius: 5px;
  span {
    font-size: 13px;
    color: #909399;
  }
}
.confirm {
  height: 36px;
  width: 56px;
  padding-left: 15px;
  font-size: 12px;
}
.cancel {
  height: 36px;
  width: 56px;
  padding-left: 15px;
  font-size: 12px;
}
</style>
